import Vue from 'vue'
import App from './App'
import i18n from './i18n/i18n'
import store from './store/index.js'
// 引入gaTracker.js
import {
	GoogleAnalytics,
	HitBuilders
} from "@ouduidui/ga-tracker";

// 路由拦截
import {
	router,
	RouterMount
} from './router/index.js'

import {
	http
} from './utils/http.js'
// import './utils/addInterceptor.js'
// 注册全局API
const gaTracker = {
	instance: null,
	getInstance() {
		if (!this.instance) {
			// 初始化GoogleAnalytics Tracker
			this.instance = GoogleAnalytics.getInstance()
				.setAppName('CTWApp') // 设置名称
				.setAppVersion('1.0.0') // 设置版本号
				.newTracker('UA-126541034-1'); // 设置跟踪ID
			// // #ifdef MP-WEIXIN
			// // 这里是小程序需要填写的使用自己的合法域名做跟踪数据转发
			// this.instance.setTrackerServer("https://ga-proxy.example.com");
			// // #endif
		}
		return this.instance;
	},
}
Vue.prototype.$gaTracker = gaTracker;

//google统计封装 
const gaFun = () => {
	const analytic = gaTracker.getInstance()
	var pages = getCurrentPages() // 获取栈实例
	// console.log(pages[pages.length - 1]['route'])
	// console.log(pages[pages.length - 1]['$page']['fullPath'])
	let currentPage = pages[pages.length - 1]['$page']['fullPath'] //当前页面路径(带参数)
	analytic.setScreenName("/pwa#" + currentPage);
	// analytic.send(new HitBuilders.ScreenViewBuilder().build()); //上报
	analytic.send(new HitBuilders.ScreenViewBuilder().build()); //上报
}
Vue.prototype.$ga = gaFun

Vue.config.productionTip = false

Vue.prototype.$http = http // 请求
Vue.prototype._i18n = i18n
Vue.prototype.$store = store // vuex
// 时间过滤器
Vue.filter('dateFormat', function(value) {
	if (!value) {
		return
	}
	const dt = new Date(value)
	const y = dt.getFullYear()
	const m = (dt.getMonth() + 1 + '').padStart(2, 0)
	const d = (dt.getDate() + '').padStart(2, 0)
	const hh = (dt.getHours() + '').padStart(2, 0)
	const mm = (dt.getMinutes() + '').padStart(2, 0)
	const ss = (dt.getSeconds() + '').padStart(2, 0)
	return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
// 时间过滤器2
Vue.filter('dateFormat2', function(value) {
	if (!value) {
		return
	}
	switch (uni.getSystemInfoSync().platform) {
		case 'android':
			const dt = new Date(value)
			const y = dt.getFullYear()
			const m = (dt.getMonth() + 1 + '').padStart(2, 0)
			const d = (dt.getDate() + '').padStart(2, 0)
			return `${y}-${m}-${d}`
			break;
		case 'ios':
			var reg = new RegExp('-', "g")
			const dt2 = new Date(value.replace(reg, '/'))
			const y2 = dt2.getFullYear()
			const m2 = (dt2.getMonth() + 1 + '').padStart(2, 0)
			const d2 = (dt2.getDate() + '').padStart(2, 0)
			return `${y2}-${m2}-${d2}`
			break;
	}

})
// 三位分节法

Vue.filter('computedNum', function(nstr) {
	// .toLocaleString()
	if (nstr != '' && nstr != null) {
		nstr += '';
		var x = nstr.split('.');
		var x1 = x[0];
		var x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	} else {
		return '-'
	}
})
// 去掉逗号转数字
Vue.filter('removeComma', function(num) {
	if (!num) {
		return false
	}
	if (typeof num === 'string') {
		const ter = num.replace(/,/g, '');
		return ter;
	} else {
		return num;
	}
})

App.mpType = 'app'
const app = new Vue({
	store,
	i18n,
	...App
})

// #ifdef H5
RouterMount(app, router, '#app')
// #endif

// #ifndef H5
app.$mount(); //为了兼容小程序及app端必须这样写才有效果
// #endif
// app.$mount(); //为了兼容小程序及app端必须这样写才有效果
